package com.dd.consul_consumer_feign.controller;

import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.boot.SpringApplication;  
import org.springframework.boot.autoconfigure.SpringBootApplication;  
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;  
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;  
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RestController;  

import springfox.documentation.annotations.ApiIgnore;
  

@EnableDiscoveryClient  
@RestController  
public class TestController  {  
      
    @Autowired  
    private LoadBalancerClient loadBalancer;  
      
    @Autowired  
    private DiscoveryClient discoveryClient;  
      
    /** 
     * 从所有服务中选择一个服务（轮询） 
     */  
    @RequestMapping("/discover")  
    public Object discover() {  
    	ServiceInstance si = loadBalancer.choose("consul-provider");
        return si.getUri().toString();  
    }  
      
    /** 
     * 获取所有服务  
     */  
    @RequestMapping("/services3")  
    public Object services() {  
        return discoveryClient.getInstances("consul-provider");  
    }  
}  